//
//  CSL_MoreViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "CSL_MoreViewController.h"
#import "HZ_Search_ViewController.h"
#import "CSL_mvMoreCell.h"
#import "CSL_mvMore_Model.h"
#import "CSL_Data_TagModel.h"
#import "CSL_Data_MvListModel.h"
#import "CSL_MV_dataModel.h"
#import <UIImageView+WebCache.h>
#import "CSL_Player_ViewController.h"
#import "CSL_PlayerPage_ViewController.h"//播放页面测试

@interface CSL_MoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CSL_mvMore_Model *mvMoreModel;
@property (nonatomic, strong) CSL_MV_dataModel *dataModel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *tagdataArray;
@property (nonatomic, strong) NSMutableArray *mvListArray;
@end

@implementation CSL_MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self MvRequestNet];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width/1.5, self.view.bounds.size.height/13)];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor colorWithWhite:0.411 alpha:1.000];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"MV专区";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1.000 green:0.263 blue:0.222 alpha:1.000];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"bBack64"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-sousuo-4"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];

    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    self.tagdataArray = [NSMutableArray arrayWithCapacity:0];
    self.mvListArray = [NSMutableArray arrayWithCapacity:0];
    
    
    [self CreateTableView];
}

-(void)leftBarButtonItemAction:(UIBarButtonItem *)sender {
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBarButtonItemAction:(UIBarButtonItem *)sender{
    HZ_Search_ViewController *searchViewController = [[HZ_Search_ViewController alloc] init];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.835 green:0.801 blue:0.849 alpha:1.000];
    [self.navigationController pushViewController:searchViewController animated:YES];
}

#pragma mark - 创建tableView
-(void)CreateTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.view.bounds.size.height/11) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CSL_mvMoreCell class] forCellReuseIdentifier:@"CSL_mvMoreCellIdentifier"];
    [self.view addSubview:_tableView];
}

#pragma mark - cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mvMoreModel.data.count;
}

#pragma mark - cell 赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CSL_mvMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CSL_mvMoreCellIdentifier"];
    _dataModel = _mvMoreModel.data[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@", _dataModel.title];
    NSString *dstr = [str substringToIndex:2];
    cell.timeLabel.text = dstr;
    NSString *mstr = [str substringFromIndex:3];
    cell.mtimeLabel.text = mstr;
    cell.descLabel.text = _dataModel.desc;
    cell.array = _dataModel.tag;
    NSNumberFormatter *number = [[NSNumberFormatter alloc] init];
    cell.numberLabel.text = [number stringFromNumber:_dataModel.bulletCount];
    cell.mvListArray = _dataModel.mvList;
   
    return cell;
}


#pragma mark - cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height/2.5;
}

#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _dataModel = _mvMoreModel.data[indexPath.row];
    CSL_Data_MvListModel *listModel = [[CSL_Data_MvListModel alloc] init];
    if (_dataModel.mvList.count > 1) {
        listModel = _dataModel.mvList[0];
        CSL_Player_ViewController *playerController = [[CSL_Player_ViewController alloc] initWithCSL_Data_MvListModel:listModel];
        [self presentViewController:playerController animated:YES completion:^{}];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"这个没有，看别的去" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:alert animated:true completion:nil];
    }
//    CSL_PlayerPage_ViewController *playerPage_ViewController = [[CSL_PlayerPage_ViewController alloc] init];
//    [self presentViewController:playerPage_ViewController animated:YES completion:^{}];

    CSL_Player_ViewController *playerController = [[CSL_Player_ViewController alloc] initWithCSL_Data_MvListModel:listModel];
    [self presentViewController:playerController animated:YES completion:^{}];
   
    NSNotificationCenter *hiddenCollec = [NSNotificationCenter defaultCenter];
    
    [hiddenCollec postNotificationName:@"hiddenCollec" object:nil userInfo:nil];
}

#pragma mark - 网络请求
-(void)MvRequestNet{
    [HSAFNetTool GETNetWithUrl:@"http://api.dongting.com/channel/channel/mvs?size=6&page=1"body:nil headerFile:nil response:HSJSON success:^(id result) {
        
    self.mvMoreModel = [[CSL_mvMore_Model alloc] initWithDataSource:result];
            [_tableView reloadData];
    
} failure:^(NSError *error) {
    
}];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
